﻿using System;
using System.Collections.Generic;
using Epitech.Krisk.Xna.ScreenManagerLib.ScreenManager;
using Epitech.Krisk.Xna.UI.Game.Utils;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace Epitech.Krisk.Xna.UI.Game.InGame.Gameplay
{
    public class Territory : AGameObject
    {
        public bool IsSelected { get; set; }
        private readonly Color _zoneColor;
        public String Name { get; set; }
        public Continent Continent { get; set; }
        public Player Owner { get; set; }
        public event EventHandler Clicked;
        public List<Territory> Neighbors { get; set; }
        private Vector2 _center;
        Texture2D _background;
        Texture2D _pin;

        public Territory(Color color, Vector2 center)
        {
            _zoneColor = color;
            _center = center;
            IsSelected = false;
        }

        public static List<Territory> Territories { get; set; }

        public ToolTip ToolTip
        {
            get { return _toolTip; }
            set { _toolTip = value; }
        }

        public static void RecupListTerritory()
        {
            if (Continent.Continents != null)
            {
                Territories = new List<Territory>();
                foreach (KeyValuePair<string, Continent> continent in Continent.Continents)
                {
                    Territories.AddRange(continent.Value.Territories.ToArray());
                }
            }
        }

        public override void LoadContent()
        {
            _background = Content.Load<Texture2D>("Game/Zone/Zone");
            _pin = Content.Load<Texture2D>("Game/Pin");
        }

        private ToolTip _toolTip = null;

        public override void HandleInput(InputState input)
        {
            Rectangle sourceRectangle =
                new Rectangle((int)RenderMovingBackground.Areabg.X + (int)((double)input.MousePosition().X / (double)RenderMovingBackground.Dest.Width * (double)RenderMovingBackground.Areabg.Width),
                              (int)RenderMovingBackground.Areabg.Y + (int)((double)input.MousePosition().Y / (double)RenderMovingBackground.Dest.Height * (double)RenderMovingBackground.Areabg.Height),
                              1, 1);

            Color[] retrievedColor = new Color[1];

            if (sourceRectangle.X >= 0
                && sourceRectangle.Y >= 0
                && sourceRectangle.Width < _background.Width
                && sourceRectangle.Height < _background.Height)
            {
                _background.GetData<Color>(0, sourceRectangle, retrievedColor, 0, 1);

                if (retrievedColor[0] == _zoneColor)
                {
                    PlayerIndex tmp;
                    ToolTip = new ToolTip();
                    ToolTip.Territory = this;
                    ToolTip.Content = Content;
                    ToolTip.ViewPort = ViewPort;
                    ToolTip.SpriteBatch = SpriteBatch;
                    ToolTip.LoadContent();

                    if (input.IsNewMouseButtonDown(MouseButton.Left) ||
                        input.IsNewButtonPress(Buttons.A, null, out tmp) ||
                        input.KinectClick)
                        Clicked(this, null);
                }
                else
                {
                    ToolTip = null;
                }
                if (ToolTip != null)
                    ToolTip.HandleInput(input);
            }
        }


        public override void Draw(GameTime gameTime)
        {
            Vector2 newCenter = new Vector2(((_center.X - RenderMovingBackground.Areabg.X) * RenderMovingBackground.Dest.Width) / RenderMovingBackground.Areabg.Width - (_pin.Width / 2),
                                            ((_center.Y - RenderMovingBackground.Areabg.Y) * RenderMovingBackground.Dest.Height) / RenderMovingBackground.Areabg.Height - (_pin.Width / 2));

            Color alpha = new Color(Owner.Color.ToVector3()) {A = (byte) (IsSelected ? 128 : 255)};
            SpriteBatch.Begin();
            SpriteBatch.Draw(_pin, newCenter, alpha);
            SpriteBatch.End();
        }
    }
}
